package com.jinyu.jyly.ui.mine.setting.changemobile;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.jinyu.jyly.R;
import com.jinyu.jyly.base.BaseActivity;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.utils.AES;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 修改手机号
 */
public class ChangeMobileActivity extends BaseActivity<ChangeMobilePresenter> implements ChangeMobileContract.View {

    @BindView(R.id.fake_status_bar)
    View fakeStatusBar;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rl_title_view)
    RelativeLayout rlTitleView;
    @BindView(R.id.tv_old_mobile)
    TextView tvOldMobile;
    @BindView(R.id.et_new_mobile)
    EditText etNewMobile;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.btn_sub)
    Button btnSub;

    private ChangeMobilePresenter presenter;
    private int mSmsId;

    @Override
    public int getLayout() {
        return R.layout.activity_change_mobile;
    }

    @Override
    protected ChangeMobilePresenter getPresenter() {
        if (presenter == null) {
            presenter = new ChangeMobilePresenter(this, this);
        }
        return presenter;
    }

    @Override
    protected void initEventAndData() {
        QMUIStatusBarHelper.translucent(this);
        QMUIStatusBarHelper.setStatusBarDarkMode(this);
    }

    @OnClick({R.id.iv_back, R.id.tv_get_code, R.id.btn_sub})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_get_code:
                //修改手机号
                presenter.siteMakeSms();

//                String moblile = etNewMobile.getText().toString().trim();
//                if (TextUtils.isEmpty(moblile)) {
//                    showToast("手机号不能为空");
//                    return;
//                }
//                try {
//                    JSONObject jsonObject = new JSONObject();
//                    jsonObject.put("mobile", moblile);
//                    jsonObject.put("template", "findpassword");
//                    String pamam = AES.encryptToHexString(jsonObject.toString());
//                    presenter.siteMakeSms();
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
                break;
            case R.id.btn_sub:
                String oldMobile = tvOldMobile.getText().toString().trim();
                String newMobile = etNewMobile.getText().toString().trim();
                String code = etCode.getText().toString().trim();

                if (TextUtils.isEmpty(oldMobile)) {
                    showToast("原手机号不能为空");
                    return;
                }

                if (TextUtils.isEmpty(newMobile)) {
                    showToast("新手机不能为空");
                    return;
                }

                if (TextUtils.isEmpty(code)) {
                    showToast("验证码不能为空");
                    return;
                }

                try {
                    //修改手机号
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("mobile", oldMobile);
                    jsonObject.put("newMobile", newMobile);
                    jsonObject.put("code", code);
                    String param = AES.encryptToHexString(jsonObject.toString());
                    presenter.changeMobile(param);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onFailer(Throwable throwable) {
    }

    @Override
    public void onChangeMobile(AllInfoBean infoBean) {
        switch (infoBean.getStatus()) {
            case 200:
                String beanData = infoBean.getData();
                String result = null;
                try {
                    result = AES.decrypt(beanData);
                    showToast("修改手机号成功");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                showToast(infoBean.getMsg());
                break;
            default:
                break;
        }
    }

    /**
     * 登录后发送短信
     * @param infoBean
     */
    @Override
    public void onSiteMakeSms(AllInfoBean infoBean) {
        switch (infoBean.getStatus()) {
            case 200:
                try {
                    String data = infoBean.getData();
                    String result = AES.decrypt(data);
                    JSONObject jsonObject = new JSONObject(result);
                    mSmsId = jsonObject.optInt("id");
                    downTimer.start();
                    tvGetCode.setEnabled(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                tvGetCode.setEnabled(true);
                tvGetCode.setText("获取验证码");
                showToast(infoBean.getMsg());
                break;
            default:
                break;
        }
    }

    @Override
    public void onChangeContractMobile(AllInfoBean infoBean) {

    }


    CountDownTimer downTimer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long arg0) {
            tvGetCode.setText("重新发送(" + arg0 / 1000 + ")");
            tvGetCode.setEnabled(false);
        }

        @Override
        public void onFinish() {
            tvGetCode.setEnabled(true);
            tvGetCode.setText("获取验证码");
        }
    };



}
